//
//  HDDeclare.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/7.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDDeclare.h"
#import "sys/utsname.h"
#import "SDImageCache.h"
#import <SystemConfiguration/CaptiveNetwork.h>
#import <sys/utsname.h>
#import "HD_LY_TipView.h"

@implementation HDDeclare

#pragma mark -------- Init Methods --------

+ (id)sharedDeclare
{
    static id sharedDeclare;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedDeclare = [[self alloc] init];
    });
    return sharedDeclare;
}

- (id)init
{
    self = [super init];

    if(self){
        _beaconFlag = NO;
    }
    return self;
}

+ (BOOL)isValidBeacon:(NSInteger)beaconNum
{
    BOOL isValid = NO;
    if (beaconNum > 0 && beaconNum < 1000)
        isValid = YES;
    
    return isValid;
}

- (void)createMapPath:(NSString *)webMapPath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = NO;
    NSError *isError = nil;
    BOOL existed = [fileManager fileExistsAtPath:webMapPath isDirectory:&isDir];
    if (isDir == YES && existed == YES) {
        [fileManager removeItemAtPath:webMapPath error:&isError];
    }
    
    [fileManager createDirectoryAtPath:webMapPath withIntermediateDirectories:YES attributes:nil error:nil];
}

- (void)clearTmpPics // 清理SDWebImg缓存
{
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];//可有可无
}

- (void)setUserPhoneID:(NSString *)userPhoneID
{
    if (userPhoneID==nil || userPhoneID.length ==0)
        return;
}
- (NSString*)deviceVersion
{
    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone4S";
    if ([deviceString isEqualToString:@"iPhone5,2"]||[deviceString isEqualToString:@"iPhone5,4"])    return @"iPhone5";
    if ([deviceString isEqualToString:@"iPhone6,2"])    return @"iPhone5S";
    if ([deviceString isEqualToString:@"iPhone7,1"]||[deviceString isEqualToString:@"iPhone7,2"])    return @"iPhone6";
    if ([deviceString isEqualToString:@"iPhone8,1"]||[deviceString isEqualToString:@"iPhone8,2"])    return @"iPhone6s";
    return deviceString;
}

- (void)setLanguageType:(Language_Type)languageType
{
    switch (languageType) {
        case kLanguage_Type_Chinese:
            _language = kLanuage_CHINESE;
            break;
        case kLanguage_Type_English:
            _language = kLanuage_ENGLISH;
            break;
        case kLanguage_Type_Japanese:
            _language = kLanuage_JAPANESE;
            break;
        case kLanguage_Type_Korean:
            _language = kLanuage_KOREAN;
            break;
        case kLanguage_Type_French:
            _language = kLanuage_FRENCH;
            break;
        case kLanguage_Type_Germany:
            _language = kLanuage_GERMANY;
            break;
        case kLanguage_Type_Spanish:
            _language = kLanuage_SPANISH;
            break;
            
        default:
            _language = kLanuage_CHINESE;
            break;
    }
    //保存翻译词条
    [self saveTranslateDic];
}

- (NSString*)nowLanguage
{
    return _language;
} // 当前语种，大写

- (Language_Type)languageIndex
{
    Language_Type languageType = kLanguage_Type_Chinese;
    if ([_language isEqualToString:@"ENGLISH"])
    {
        languageType = kLanguage_Type_English;
    }
    
    else if ([_language isEqualToString:@"JAPANESE"])
    {
        languageType = kLanguage_Type_Japanese;
    }
    
    else if ([_language isEqualToString:@"KOREAN"])
    {
        languageType = kLanguage_Type_Korean;
    }
    else if ([_language isEqualToString:@"FRENCH"])
    {
        languageType = kLanguage_Type_French;
    }
    else if ([_language isEqualToString:@"GERMANY"])
    {
        languageType = kLanguage_Type_Germany;
    }
    else if ([_language isEqualToString:@"SPANISH"])
    {
        languageType = kLanguage_Type_Spanish;
    }
    
    return languageType;
}

- (NSString*)nowLanguageIndexStr // 当前语种1,2,3,4...
{
    return  [NSString stringWithFormat:@"%i", self.languageIndex];
}

- (void)getData {
    /*
    NSArray *dataArray = [Exhibit_info findAllModel];
    self.resArray = dataArray;
    
    NSArray *mapArray = [Map_base findAllModel];
    self.mapSizeArray = mapArray;
    
    NSArray *serArray = [Service_info findAllModel];
    self.serviceArray = serArray;*/
}

- (BOOL)isRouteShowMode
{
    return self.routeArray.count > 0;
}

#pragma mark----------翻译---------
- (void)saveTranslateDic{
    if (self.translateDic) {
        self.translateDic = nil;
    }
    NSString *plistFile = [NSString stringWithFormat:@"%@",[_language lowercaseString]];
    if ([plistFile isEqualToString:@""]) {
        return;
    }
    self.translateDic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:plistFile ofType:@"plist"]];
}

- (NSString *)getTranslate:(NSString*)originalString{
    NSString *resultStr = [self.translateDic objectForKey:[NSString stringWithFormat:@"%@",originalString]];
    if (resultStr == nil)
        resultStr = @"  ";
    
    return resultStr;
}

+ (NSString *)UIString:(NSString *)keyStr
{
    return [[self sharedDeclare] getTranslate:keyStr];
}

#define kWLAN_SSID_Name  @"HuNanMuseum"

//开发
//#define kWLAN_Ip_Address @"http://192.168.10.158:8110/api"//内网
//#define kNet_Ip_Address  @"http://192.168.10.158:8110/api"//外网
//#define kWLAN_Img_Address @"http://192.168.10.158:8110"//内网
//#define kNet_Img_Address  @"http://192.168.10.158:8110"//外网

//阿里云地址
#define kWLAN_Ip_Address @"http://47.93.81.30:8110/api"//内网
#define kNet_Ip_Address  @"http://47.93.81.30:8110/api"//外网
#define kWLAN_Img_Address @"http://47.93.81.30:8110"//内网
#define kNet_Img_Address  @"http://47.93.81.30:8110"//外网

//测试
#define kWLAN_Ip_Address @"http://192.168.10.41:8110/api"//内网
#define kNet_Ip_Address  @"http://192.168.10.41:8110/api"//外网:192.168.10.41:8110
#define kWLAN_Img_Address @"http://192.168.10.41:8110"//内网
#define kNet_Img_Address  @"http://192.168.10.41:8110"//外网

+ (NSString *)IP_Request_Header // 根据内外网获取http请求的前缀
{
    NSString *ipPrefixStr = kNet_Ip_Address;
    if ([[HDDeclare sharedDeclare] isWLANNet] == YES)
        ipPrefixStr = kWLAN_Ip_Address;
    
    return ipPrefixStr;
}

+ (NSString *)imagePathHeader {
    NSString *ipPrefixStr = kNet_Img_Address;
    if ([[HDDeclare sharedDeclare] isWLANNet] == YES)
        ipPrefixStr = kWLAN_Img_Address;
    
    return ipPrefixStr;
}

+ (NSString *)realSizeImagePathWithRequestPath:(NSString *)requestPath {
   
    requestPath = [NSString stringWithFormat:@"%@%@", [HDDeclare imagePathHeader],requestPath];
    return requestPath;
}

/*
 图片URL生成规则：
 域名/thumbimg/md5图片路径的第一个字符/md5图片路径的第四个字符/宽度/高度/剪裁类型/base64图片路径.auto.jpg
 
 例如：图片路径为 /uploadfiles/test/bn1.png
 生成的缩略图链接为：
 http://192.168.10.158:8110/thumbimg/6/8/500/300/33/L3VwbG9hZGZpbGVzL3Rlc3QvYm4xLnBuZw==.auto.png
 
 图片剪裁类型：
 31：按缩放比小的一边等比缩放
 32：按比例缩放后填充
 33：等比缩放后居中剪切（推荐）
 34：左上剪切
 35：右下剪切
 36：固定尺寸，图片可能变形
 
 规则是服务器定的
 */
// 根据图片返回路径计算图片url
+ (NSString *)URLImagePath:(NSString *)requestPath
                   imgSize:(CGSize)imgSize
{
   
    
    if (requestPath.length == 0)
        return nil;
    
    NSString *md5Str = [requestPath md5String];
    if (md5Str.length < 5)
        return nil;
    
    NSInteger width = imgSize.width + 0;
    NSInteger height = imgSize.height + 0;
    NSString *widthStr = [NSString stringWithFormat:@"%li", width];
    NSString *heighStr = [NSString stringWithFormat:@"%li", height];
    NSString *clipStr = @"33";
    NSString *base64pStr = [self base64StringFromText:requestPath];
    NSString *ipPrefixStr = [NSString stringWithFormat:@"%@/thumbimg",kWLAN_Img_Address];
    
    NSString *imgPath = [NSString stringWithFormat:@"%@/%@/%@/%@/%@/%@/%@.auto.png",ipPrefixStr,
                         [md5Str substringToIndex:1],
                         [md5Str substringWithRange:NSMakeRange(4, 1)],
                         widthStr,
                         heighStr,
                         clipStr,
                         base64pStr];
    
    return imgPath;
}

/**
 *  将普通字符串转换成base64字符串
 *
 *  @param text 普通字符串
 *
 *  @return base64字符串
 */
+ (NSString *)base64StringFromText:(NSString *)text {
    
    NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [data base64EncodedStringWithOptions:0];
    
    return base64String;
}
/**
 *  将base64字符串转换成普通字符串
 *
 *  @param base64 base64字符串
 *
 *  @return 普通字符串
 */
+ (NSString *)textFromBase64String:(NSString *)base64 {
    
    NSData *data = [[NSData alloc] initWithBase64EncodedString:base64 options:0];
    NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    return text;
}

- (BOOL)isWLANNet
{
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    LOG(@"Supported interfaces: %@", ifs);
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        LOG(@"%@ => %@", ifnam, info);
        if (info && [info count]) { break; }
    }
    NSString *ssid = [info objectForKey:@"SSID"];
    
    return [ssid.uppercaseString isEqualToString:kWLAN_SSID_Name.uppercaseString];
}

#pragma mark
#pragma mark -------- Tip Methods --------

+ (void)showSuccessTip:(NSString *)msg {
    if (![msg isNotBlank]) return ;
    HD_LY_TipView *showTipView = [HD_LY_TipView alertViewWithMsg:msg style:LYAlertActionStyleSuccess];
    [showTipView show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [showTipView hide];
    });
}

+ (void)showErrorTip:(NSString *)msg {
    if (![msg isNotBlank]) return ;
    HD_LY_TipView *showTipView = [HD_LY_TipView alertViewWithMsg:msg style:LYAlertActionStyleError];
    [showTipView show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [showTipView hide];
    });
}

+ (void)showMsgTip:(NSString *)msg {
    if (![msg isNotBlank]) return ;
    HD_LY_TipView *showTipView = [HD_LY_TipView alertViewWithMsg:msg style:LYAlertActionStyleMsg];
    [showTipView show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [showTipView hide];
    });
}


@end










